package com.cc.data_explore.gui.tab;

import com.cc.data_explore.gui.base.PagingControl;
import de.saxsys.mvvmfx.FxmlView;
import de.saxsys.mvvmfx.InjectViewModel;
import javafx.beans.binding.Bindings;
import javafx.beans.property.*;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableMap;
import javafx.css.PseudoClass;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * CatImportDataDialogView
 *
 * @author aa
 * @version 1.0
 * @since 2025/4/10
 */
public class CatImportDataDialogView implements FxmlView<CatImportDataDialogViewModel> {

    @FXML
    public TableView<Map<String, Object>> tableView;

    private final ObservableMap<String, String> columnMap = FXCollections.observableMap(new LinkedHashMap<>());
    @FXML
    public VBox vbox;

    private final IntegerProperty selectedRow = new SimpleIntegerProperty(-1);
    private final ObjectProperty<TableColumn<Map<String, Object>, String>> selectedCol = new SimpleObjectProperty<>();

    private static final PseudoClass ROW_HIGHLIGHT = PseudoClass.getPseudoClass("highlight-row");
    private static final PseudoClass COL_HIGHLIGHT = PseudoClass.getPseudoClass("highlight-col");

    @InjectViewModel
    private CatImportDataDialogViewModel viewModel;

    @FXML
    public void initialize() {
        Bindings.bindContent(columnMap, viewModel.getColumnMap());
        PagingControl pagingControl = new PagingControl();
        pagingControl.totalProperty().bind(viewModel.totalProperty());
        pagingControl.pageNumProperty().addListener((observable, oldValue, newValue) -> viewModel.loadData());
        pagingControl.pageSizeProperty().addListener((observable, oldValue, newValue) -> viewModel.loadData());
        viewModel.pageNumProperty().bind(pagingControl.pageNumProperty());
        viewModel.pageSizeProperty().bind(pagingControl.pageSizeProperty());
        vbox.getChildren().add(pagingControl);
        tableView.setItems(viewModel.getItems());
        tableView.getSelectionModel().setCellSelectionEnabled(true);
        tableView.setRowFactory(tv -> {
            TableRow<Map<String, Object>> row = new TableRow<>();
            row.pseudoClassStateChanged(ROW_HIGHLIGHT, false);
            row.indexProperty().addListener((obs, oldIndex, newIndex) -> row.pseudoClassStateChanged(ROW_HIGHLIGHT, newIndex.intValue() == selectedRow.get()));
            selectedRow.addListener((obs, oldRow, newRow) -> row.pseudoClassStateChanged(ROW_HIGHLIGHT, row.getIndex() == newRow.intValue()));
            return row;
        });
        tableView.getSelectionModel().getSelectedCells().addListener((ListChangeListener<TablePosition>) change -> {
            if (change.next()) {
                if (!change.getList().isEmpty()) {
                    TablePosition<Map<String, Object>, String> pos = change.getList().getFirst();
                    selectedRow.set(pos.getRow());
                    selectedCol.set(pos.getTableColumn());
                } else {
                    selectedRow.set(-1);
                    selectedCol.set(null);
                }
            }
        });
        setCol();
    }

    public void setCol() {
        columnMap.forEach((k, v) -> {
            TableColumn<Map<String, Object>, String> column = new TableColumn<>(k);
            column.setSortable(false);
            column.setPrefWidth(120.0);
            column.setCellValueFactory(param -> {
                Object o = param.getValue().get(v);
                return new SimpleStringProperty(o.toString());
            });
            column.setCellFactory(param -> new TableCell<>() {
                {
                    selectedCol.addListener((obs, oldCol, newCol) -> pseudoClassStateChanged(COL_HIGHLIGHT, column.equals(newCol)));
                }

                @Override
                protected void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);
                    if (empty || item == null) {
                        setText(null);
                    } else {
                        setText(item);
                    }
                    pseudoClassStateChanged(COL_HIGHLIGHT, column.equals(selectedCol.get()));
                }
            });
            tableView.getColumns().add(column);
        });
    }
}
